package com.sap.mobile.maf.samples.android.mafprovisioningsample;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SampleTabContent3 extends Activity {

	Bitmap mImageBitmap;
	ImageView mImageView;
	String mCurrentPhotoPath;
	Uri muri;
	String filename = "rece";
	String photopath;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		LinearLayout ll = new LinearLayout(this);
		TextView tv = new TextView(this);
		tv.setText("Hello Tab3");

		ll.addView(tv);
		setContentView(ll);
		
		try {
			dispatchTakePictureIntent(100);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
	}
	
	private Uri getTempFile() {


        File root = new File(Environment.getExternalStorageDirectory(), "Directory");
        if (!root.exists()) {
            root.mkdirs();
        }
             File file;

             file = new File(root,filename+".jpeg" );

             muri = Uri.fromFile(file);
             photopath=muri.getPath();

              return muri;

           }
	
	private File createImageFile() throws IOException {
	    // Create an image file name
	    String timeStamp = 
	        new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
	    String imageFileName = "rece" + timeStamp + "_";
	    File image = File.createTempFile(
	        imageFileName, 
	        "rece"
	    );
	    mCurrentPhotoPath = image.getAbsolutePath();
	    return image;
	}
	
	private void dispatchTakePictureIntent(int actionCode) throws IOException {
		
		Intent photoPickerIntent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, getTempFile());

        photoPickerIntent.putExtra("outputFormat",Bitmap.CompressFormat.JPEG.toString());
        photoPickerIntent.putExtra("return-data", true);
        startActivityForResult(photoPickerIntent,100);
	}
	
	private void galleryAddPic() {
	    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
	    File f = new File(mCurrentPhotoPath);
	    Uri contentUri = Uri.fromFile(f);
	    mediaScanIntent.setData(contentUri);
	    this.sendBroadcast(mediaScanIntent);
	}
	
	private void handleSmallCameraPhoto(Intent intent) {
	    Bundle extras = intent.getExtras();
	    mImageBitmap = (Bitmap) extras.get("data");
	    mImageView.setImageBitmap(mImageBitmap);
	    galleryAddPic();
	    
	}
	
	@Override
	protected void onActivityResult (int requestCode, int resultCode, Intent data){
		//handleSmallCameraPhoto(data);
	}

}
